package com.shortchainsystem.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shortchainsystem.model.base.Result;
import com.shortchainsystem.model.domain.link.DO.LoginUserDO;
import com.shortchainsystem.model.domain.link.DO.UserDO;
import com.shortchainsystem.model.domain.user.User;
import com.shortchainsystem.model.exception.CustomCodeEnum;
import com.shortchainsystem.user.mapper.UserMapper;
import com.shortchainsystem.user.service.UserService;
import com.shortchainsystem.util.AppJwtUtil;
import com.shortchainsystem.util.RandomUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.Cookie;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public Result login(LoginUserDO loginUserDO) {
        // 1、判断是否为空
        if (loginUserDO == null || StringUtils.isEmpty(loginUserDO.getPhone()) || StringUtils.isEmpty(loginUserDO.getPassword())) {
            return Result.errorResult(CustomCodeEnum.NULL_ERROR);
        }
        // 2、根据用户名查询用户信息
        User userData = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getPhone, loginUserDO.getPhone()));
        if (userData == null) {
            return Result.errorResult(CustomCodeEnum.USER_NOT_EXIST_ERROR);
        }
        // 3、比对密码
        String passwordInput = DigestUtils.md5DigestAsHex((loginUserDO.getPassword()).getBytes());
        if (!passwordInput.equals(userData.getPassword())) {
            return Result.errorResult(CustomCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        // 4、保存token
        Cookie cookieId = new Cookie("token", AppJwtUtil.getToken(userData.getId()));

        Map<String, Object> map = new HashMap<>();
        userData.setPassword(loginUserDO.getPassword());
        map.put("token", cookieId.getValue());
        map.put("user", userData);
        return Result.okResult(map);
    }

    @Override
    public Result loginout() {
        return Result.okResult(CustomCodeEnum.SUCCESS);
    }

    @Override
    public Result register(UserDO userDO) {
        // 1、判断是否为空
        if(userDO==null || StringUtils.isEmpty(userDO.getUsername()) || StringUtils.isEmpty(userDO.getPassword())
                || StringUtils.isEmpty(userDO.getBirthDate()) || StringUtils.isEmpty(userDO.getPhone())
                || StringUtils.isEmpty(userDO.getEmail()))
            return Result.errorResult(CustomCodeEnum.NULL_ERROR);
        // 2、判断用户是否存在
        User userdata = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getPhone, userDO.getPhone()));
        if(userdata!=null && userdata.getPhone().equals(userDO.getPhone())){
            return Result.errorResult(CustomCodeEnum.USER_EXIST_ERROR);
        }
        // 4、添加一条新用户
        userdata = new User();
        userDO.setId(RandomUtil.getRandom(10));
        userDO.setPassword(DigestUtils.md5DigestAsHex(userDO.getPassword().getBytes()));
        BeanUtils.copyProperties(userDO, userdata);
        int i = userMapper.insert(userdata);
        return i>0 ? Result.okResult(CustomCodeEnum.SUCCESS) : Result.errorResult(CustomCodeEnum.SERVER_ERROR);
    }

    @Override
    public Result edit(UserDO userDO) {
        // 1、判断是否为空
        if(userDO==null || StringUtils.isEmpty(userDO.getUsername()) || StringUtils.isEmpty(userDO.getPassword())
                || StringUtils.isEmpty(userDO.getBirthDate()) || StringUtils.isEmpty(userDO.getPhone())
                || StringUtils.isEmpty(userDO.getEmail()))
            return Result.errorResult(CustomCodeEnum.NULL_ERROR);
        // 2、密码加密
        userDO.setPassword(DigestUtils.md5DigestAsHex(userDO.getPassword().getBytes()));
        // 3、修改用户
        User user = new User();
        BeanUtils.copyProperties(userDO, user);
        userMapper.updateById(user);
        return Result.okResult(CustomCodeEnum.SUCCESS);
    }
}
